package com.haiyi.software.app;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.haiyi.software.app.delegate.FragmentGifDelegate;
import com.haiyi.software.tools.L;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewManager;
import android.view.ViewParent;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class FragmentGifGrid extends Fragment {

	private View thisview;

	private GridView gridview_gif;

	private List<Map<String, String>> faseList;

	private String gifKey;

	private String packetName;

	private FragmentGifDelegate fragmentGifDelegate;

	public FragmentGifDelegate getFragmentGifDelegate() {
		return fragmentGifDelegate;
	}

	public void setGifKey(String gifKey) {
		this.gifKey = gifKey;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		thisview = inflater.inflate(R.layout.fragment_gif_grid, null);

		initData();

		initComp();

		return thisview;
	}

	private void initData() {

		this.packetName = getActivity().getPackageName();

		this.faseList = MainApplication.getInstance().getmGifMap()
				.get(this.gifKey);

		this.fragmentGifDelegate = MainApplication.getInstance().getHiChat();

		L.i("facelist", faseList.toString());
	}

	private void initComp() {
		// TODO 自动生成的方法存根
		this.gridview_gif = (GridView) thisview
				.findViewById(R.id.gridview_gifList);

		this.gridview_gif.setOnItemClickListener(listener);

		MyAdapter adapter = new MyAdapter(thisview.getContext());

		gridview_gif.setAdapter(adapter);
	}

	private OnItemClickListener listener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO 自动生成的方法存根
			if (fragmentGifDelegate != null) {

				L.i("faseList.get(position)", faseList.get(position).toString());

				fragmentGifDelegate.SendGifFaceToYou(faseList.get(position)
						.get("string"));
			}
		}

	};

	private class MyAdapter extends BaseAdapter {

		private Context context;

		private LayoutInflater inflater;

		Resources res = getResources();

		public MyAdapter(Context context) {

			this.context = context;
			this.inflater = LayoutInflater.from(context);

		}

		@Override
		public int getCount() {
			// TODO 自动生成的方法存根
			return faseList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO 自动生成的方法存根
			return faseList.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO 自动生成的方法存根
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO 自动生成的方法存根
			convertView = inflater.inflate(R.layout.item_gif, null);

			GifImageView gifimg = (GifImageView) convertView
					.findViewById(R.id.img_gifshow);

			TextView textView_gifName = (TextView) convertView
					.findViewById(R.id.textView_gifName);

			textView_gifName.setText(faseList.get(position).get("name"));

			GifDrawable gifDrawable;
			try {

				String fstring = faseList.get(position).get("string");

				Integer ident = res.getIdentifier(fstring, "raw", packetName);

				Resources rs = getResources();

				gifDrawable = new GifDrawable(rs, ident);

				gifDrawable.seekTo(1);
				gifDrawable.stop();

				gifimg.setImageDrawable(gifDrawable);

				RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
						150, 150);

				gifimg.setLayoutParams(params);

			} catch (NotFoundException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}

			return convertView;
		}
	}

}
